<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="115px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="项目名称:" prop="xmName">
            <el-input
              v-model="queryParams.xmName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目编号:" prop="xmBh">
            <el-input
              v-model="queryParams.xmBh"
              placeholder="请输入项目编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目状态:" prop="xmZt">
            <el-select v-model="queryParams.xmZt" placeholder="请选择项目状态" clearable>
              <el-option
                v-for="dict in dict.type.kylx_xmzt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否可加计扣除:" prop="sfKJjkc">
            <el-select v-model="queryParams.sfKJjkc" placeholder="请选择是否可加计扣除" clearable>
              <el-option
                v-for="dict in dict.type.sf"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="归集额大于等于:" prop="dyDnFse">
            <el-input type="number"
              v-model="queryParams.dyDnFse"
              placeholder="请输入归集额大于等于"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="归集额小于等于:" prop="xyDnFse">
            <el-input type="number"
              v-model="queryParams.xyDnFse"
              placeholder="请输入归集额小于等于"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-form :model="queryParams" size="mini" label-width="170px">
      <el-row :gutter="10" class="mb8" style="display: flex; justify-content: flex-end;margin-bottom: -8px">
        <!--        <el-col :span="1.5">-->
        <!--          <el-button-->
        <!--            type="primary"-->
        <!--            icon="el-icon-plus"-->
        <!--            size="mini"-->
        <!--            @click="handleAdd"-->
        <!--            v-hasPermi="['jjkc:qyjbxx:add']"-->
        <!--          >新增-->
        <!--          </el-button>-->
        <!--        </el-col>-->
        <!--          <el-col :span="1.5">-->
        <!--            <el-button-->
        <!--              type="primary"-->
        <!--              icon="el-icon-plus"-->
        <!--              size="mini"-->
        <!--              @click="handleAdds"-->
        <!--            >新增12-->
        <!--            </el-button>-->
        <!--          </el-col>-->
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出Excel
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="kJikc"
          >可加计扣除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="bkJikc"
          >不可加计扣除
          </el-button>
        </el-col>
        <el-col :span="1.5" style="margin-left: auto;">
          <el-form-item label="加计扣除鉴定计划:" prop="xmZt">
            <vxe-select @change="getList" size="mini" v-model="queryParams.nd" placeholder="请选择加计扣除鉴定计划">
              <vxe-option v-for="item in ywJhList" :key="item.nd" :value="item.nd"
                          :label="item.jhLb"></vxe-option>
            </vxe-select>
          </el-form-item>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              height="300" class="fixedTableTwo" v-loading="loading" :data="yfxmsxList"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column show-overflow-tooltip label="年度" align="center">
        <template slot-scope="scope">
          <span>{{ queryParams.nd }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目名称" align="center" prop="xmName"/>
      <el-table-column show-overflow-tooltip label="项目编号" align="center" prop="xmBh"/>
      <el-table-column show-overflow-tooltip label="负责人" align="center" prop="fzName"/>
      <el-table-column show-overflow-tooltip label="项目分类" align="center" prop="xmFl"/>
      <el-table-column show-overflow-tooltip label="承担单位" align="center" prop="cdDw"/>
      <el-table-column show-overflow-tooltip label="项目状态" align="center" prop="xmZt"/>
      <el-table-column show-overflow-tooltip label="计划开始时间" align="center" prop="jhKsTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jhKsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="计划完成时间" align="center" prop="jhJsTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jhJsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目总金额(万元)" align="center" prop="xmZje"/>
      <el-table-column show-overflow-tooltip label="当年发生额(元)" align="center" prop="dnFse"/>
      <el-table-column show-overflow-tooltip label="是否可加计扣除" align="center" prop="sfKJjkc"/>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {selectYfxmsxList, updateKJjkc} from "@/api/jjkc/yfxmsx";
import {addJdjh, updateJdjh} from "@/api/jjkc/jdjh";
import {selectBfyYwJhList} from "@/api/xtgl/ywjh";

export default {
  name: "Yfxmsx", //研发项目筛选
  dicts: ['kylx_xmzt', 'year', 'sf'],
  data() {
    return {
      ywJhList: null, //业务计划列表
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nd: new Date().getFullYear().toString(),
        xmName: null,
        xmBh: null,
        xmZt: null,
        dyDnFse: null,
        xyDnFse: null,
        sfKJjkc: null,
      },
      selectedRow: null, // 保存当前选中的行
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      yfxmsxList: [], //研发项目筛选表格信息
      // 显示搜索条件
      showSearch: true,

    }
  },
  created() {
    this.getList()
    this.selectYwJhList()
  },
  methods: {
    //不分页查询业务计划中的加计扣除计划列表
    selectYwJhList() {
      selectBfyYwJhList().then(res => {
        this.ywJhList = res.data
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jjkc/yfxmsx/export', {
        ...this.queryParams
      }, `研发项目筛选_${this.queryParams.nd}年加计扣除活动.xlsx`)
    },
    //可加计扣除
    kJikc() {
      const row = this.selectedRow; // 获取当前选中的行
      console.log(row.jdjhId)
      let xmsbId = row.xmsbId
      let nd = this.queryParams.nd
      if (row.sfKJjkc == null) {
        let form = {
          jdjhId: null,
          xmsbId: xmsbId,
          nd: nd,
          sfKJjkc: '是',
        };
        addJdjh(form).then(response => {
          this.getList();
        });
      }else if (row.sfKJjkc == '否') {
        let form = {
          jdjhId: row.jdjhId,
          xmsbId: xmsbId,
          nd: nd,
          sfKJjkc: '是',
        };
        updateJdjh(form).then(response => {
          this.getList();
        });
      }
    },
    //不可加计扣除
    bkJikc() {
      const row = this.selectedRow; // 获取当前选中的行
      let xmsbId = row.xmsbId
      let nd = this.queryParams.nd
      if (row.sfKJjkc == null) {
        let form = {
          jdjhId: null,
          xmsbId: xmsbId,
          nd: nd,
          sfKJjkc: '否',
        };
        addJdjh(form).then(response => {
          this.getList();
        });
      }else if (row.sfKJjkc == '是') {
        let form = {
          jdjhId: row.jdjhId,
          xmsbId: xmsbId,
          nd: nd,
          sfKJjkc: '否',
        };
        updateJdjh(form).then(response => {
          this.getList();
        });
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    getList() {
      this.loading = true;
      selectYfxmsxList(this.queryParams).then(res => {
        this.yfxmsxList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.xmsbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

  }
}
</script>

<style scoped>

</style>
